package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.constants.CommonConstant;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.crawlers.article.XinLangBoKeParser;
import com.dmcb.trade.business.entities.Article;
import com.dmcb.trade.business.entities.Author;
import com.dmcb.trade.business.mappers.ArticleCrawlMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Created by 63221_000 on 2016/8/14 0014.
 */
@Service
public class XinLangBoKeCrawler extends BaseCrawler {
    Logger logger = LoggerFactory.getLogger(JianshuCrawler.class);
    @Autowired
    private XinLangBoKeParser xinLangBoKeParser;
    @Autowired
    private ArticleCrawlMapper articleCrawlMapper;

    @Override
    protected String url() {
        return CrawlConstant.XINLANGBOKE_URL;
    }

    @Override
    protected int tag() {
        return CrawlConstant.XINLANGBOKE_TAG;
    }

    @Override
    protected String avatarSelect() {
        return "div.info_img>img";
    }

    @Override
    protected String channelNameSelect() {
        return "strong#ownernick";
    }

    @Override
    protected String authorLink() {
        return "div.blognavInfo>span>a.on";
    }

    @Override
    protected String listSelect() {
        return "div.articleList>div";
    }

    @Override
    protected String titleSelect() {
        return "span.atc_title>a";
    }

    @Override
    protected String authorSelect() {
        return "strong#ownernick";
    }

    @Override
    protected JsonResult crawl(int authorId, String link, Author author, Document doc, boolean all) {
        // 爬取文章
        int page = 1;
        int totalAll = 0;
        doc = Jsoup.parse(webClientUtil.getPage(link).asXml(), baseUrl(link));
        Element element = doc.select("div.blognavInfo>span").get(1);
        link = element == null?null:element.select("a").attr("href");
        doc = Jsoup.parse(webClientUtil.getPage(link).asXml(), baseUrl(link));
        while (page <= CrawlConstant.CRAWL_PAGE_TOTAL) {
            page++;
            JsonResult result = iterate2(authorId, doc);
            if (!result.isSuccess()) {
                return result;
            }
            totalAll += result.getInt();
            if (result.getInt() == 0 && page >50) {
                break;
            }
            link = link.substring(0,link.lastIndexOf("_")+1)+page+".html";
            doc = Jsoup.parse(webClientUtil.getPage(link).asXml(), baseUrl(link));
        }
        return totalAll == 0 ? error(CrawlConstant.MSG_WARN_NO_UPDATE) : success(totalAll);
    }

    /**
     * 迭代解析、保存文章
     *
     * @return 成功保存数量
     */
    protected JsonResult iterate2(int authorId, Document doc) {
        Elements elements = doc.select(listSelect());
        if (elements.size() == 0) {
            return success(0);
        }
        int succ = 0;
        int exist = 0;
        for (Element element : elements) {
            // 获取标题、链接元素
            Element ele = element.select(titleSelect()).first();
            if (ele != null) {
                try {
                    // 校验作者、标题是否已存在
                    Article article = articleCrawlMapper.queryByAuthorTitle(authorId, ele.text().trim());
                    if (article != null) {
                        continue;
                    }
                    String href = ele.absUrl("href");
                    JsonResult result = parse(authorId, href);
                    succ += result.isSuccess() ? 1 : 0;
                } catch (Exception e) {
                    logger.error("Parser.parse error,{}", ele, e);
                    return error(CommonConstant.ABNORMAL_MSG);
                }
            }
        }
        return success(succ);
    }


    @Override
    protected JsonResult iterate(int authorId, Document doc) {
        return super.iterate(authorId, doc);
    }

    @Override
    protected JsonResult parse(int authorId, String link) throws Exception {
        return xinLangBoKeParser.parse(authorId, link);
    }

    @Override
    protected String getBody(Document doc) {
        return xinLangBoKeParser.getBody(doc);
    }
}
